<?php
class Default_Model_User extends Zend_Db_Table{

	protected $_name="tbluser";
	protected $_primary="id";
	protected $db;
	public function __construct(){
		parent::__construct();
		$this->db=Zend_Registry::get('connectDb');
	}
	public function test(){
		$select=$this->db->select()->from('tbluser AS u');
		$select->joinLeft('tblquestion AS q','q.id_user=u.id','q.*');
		$select->where('u.id=1');
		return $this->db->fetchAll($select);
		
	}
 	public function listall(){
        return $this->fetchall()->toArray();
    }
	public function count(){
		$select = $this->db->select()->from('tbluser AS u',array('COUNT(u.id) AS totalItem'));
		$result=	$this->db->fetchOne($select);
		return $result;
	}

	public function getItems($param=NULL,$option=NULL){
		$select=$this->db->select()->from('tbluser AS u');
		if($option['fliter']=='question'){	
			$select->joinLeft('tblquestion AS q','q.id_user=u.id','q.*')
					->where('u.id='.$param['id']);
			return $this->fetchAll($select);
		}
		if($option['fliter']=='asnwer'){
			$select->joinLeft('tblasnwer AS a','a.id_user=u.id','a.*')
			->where('id='.$param['id']);
			return $this->fetchAll($select);
		}

		
	}
	public function getItem($param=null){
		$data=$this->select();
		$data->where('id=?',$param['id']);
		return $this->fetchRow($data);
	}
	public function saveItem($data=null,$option=null){
		$encode = new Zendvn_Encode;
		
		if($option['do']=="add"){
			$row =$this->createRow();
			$row->email=$data['email'];
			$row->name=$data['name'];
			$row->password=$encode->password($data['password']);
			$row->expensive=$data['expensive'];
			$row->time=date("Y-m-d");
			$row->status=$data['status'];
			$row->vote=$data['vote'];
			$row->url_avatar=$data['url_avatar'];
			$row->save();
		}
		if($option['do']=="edit"){
			$row =$this->fetchRow('id=?',$data['id']);
			$row->email=$data['email'];
			$row->name=$data['name'];
			if(!empty($data['password'])){
				$row->password=$encode->password($data['password']);
			}
			$row->expensive=$data['expensive'];
			$row->time=date("Y-m-d");
			$row->status=$data['status'];
			$row->vote=$data['vote'];
			$row->url_avatar=$data['url_avatar'];
			$row->save();
		}
		
	}
	public function deleteItem($param=null,$option=null){
		$where = ' id = ' . $param['id'];
		$this->delete($where);
		if($option['do']=='mutidel'){
		// do something
		}
	}
	public function  changeStatus($param=null,$option=null){
		$this->update(array('status'=>$param['status']), $param['id']);
	}
	
	
}